package com.example.review.viewBinding;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.review.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;
    private View rootView;

    @Nullable
    @Override
    //系统会在片段首次绘制其用户界面时调用onCreateView方法
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater,container,false);
        rootView = binding.getRoot();
        return rootView;
    }

    @Override
    /*
    * onViewCreated在onCreateView执行完后立即执行
    * onViewCreated是在onCreateView后被触发的事件
    * 该方法中的参数view就是调用onCreateView返回的rootView
    * */
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "你点击了文本框", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }

    @Override
    /*
    * 当切换Fragment的时候会调用onDestoryView, 但是并不会调用onDestory方法销毁掉fragmen实例。
    * 再重新来回切换多个Fragment的时候会重新调用OnCreateView绘制界面。
    * 这样就存在多次绘制初始化视图的可能。
    * */
    public void onDestroyView() {
        super.onDestroyView();
    }
}
